import time

from selenium import webdriver
from selenium.webdriver.ie.service import Service


class DriverUtils:
    __driver = None

    # 获取浏览器驱动
    @classmethod
    def get_driver(cls):
        # 防止一个类拥有多个驱动
        if cls.__driver is None:
            service = Service("C:/Program Files/Google/Chrome/Application/chromedriver.exe")
            cls.__driver = webdriver.Chrome(service=service)
            cls.__driver.maximize_window()  # 最大化窗口
            cls.__driver.implicitly_wait(10)  # 隐式等待
        return cls.__driver

    # 关闭驱动对象
    @classmethod
    def close_driver(cls):
        # 只有该对象不为None时才能关闭
        if cls.__driver is not None:
            time.sleep(1)
            cls.__driver.close()
            cls.__driver = None # 重新将该对象设置为 None